// 实现文件复制的函数
#include <stdio.h>

int main(int argc, char *argv[])
{
    if (argc < 3) {
        printf("请输入原文件名与目标文件名.\n");
        return -1;
    }

    FILE *fp_src;
    FILE *fp_dest;

    fp_src = fopen(argv[1], "r");

    if (NULL == fp_src) {
        perror("fopen");
        return -1;
    }

    fp_dest = fopen(argv[2], "w+");

    if (NULL == fp_dest) {
        perror("fopen");
        return -1;
    }

    char buf[100] = {0};
    size_t read_num;

    while(1) {
        read_num = fread(buf, 1, 100, fp_src);

        fwrite(buf, 1, read_num, fp_dest);

        if (read_num == 0) {
            
            if (feof(fp_src)) {
                printf("to the end.\n");
                break;
            } else {
                printf("read_num = %ld\n", read_num);
                printf("read error.\n");
                return -1;
            }
        }
    }


    if (EOF == fclose(fp_src)) {
        perror("fclose");
        return -1;
    }

    if (EOF == fclose(fp_dest)) {
        perror("fclose");
        return -1;
    }

    return 0;
}
